﻿using DevExpress.Data.Browsing;
using DevExpress.XtraEditors;
using FuelDXApplication1.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace FuelDXApplication1.MksWinform
{
    public partial class FuelAdd : Form
    {
        private long count = 0; // 这是非静态字段，可以在构造函数中初始化

        public FuelAdd()
        {
            InitializeComponent();

            //Program.WatchFiles("xml", "*.txt"); // 监视当前目录下所有的txt文件

            counts = count; // 在构造函数中初始化counts
        }

        private long counts; // 这是另一个非静态字段，需要在构造函数中初始化

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            DialogResult Warning = System.Windows.Forms.MessageBox.Show($"备案号:{1}\n反馈码", "提示", MessageBoxButtons.OK, MessageBoxIcon.None);
        }

        /// <summary>
        /// 下拉框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void FuelAdd_Load(object sender, EventArgs e)
        {
            //绑定下拉框的值
            var iscaretype = await DBHelper.Query<WhetherToImportCarInFo>();
            this.lookUpEdit1.Properties.DataSource = iscaretype;
            this.lookUpEdit1.Properties.ValueMember = "WhetherToImportCarInFoId"; // 设置对应的值字段
            this.lookUpEdit1.Properties.DisplayMember = "WhetherToImportCarInFoName"; // 设置显示的字段

            //车辆种类
            var cartype = await DBHelper.Query<VehicleTypeInFo>();
            this.lookUpEdit2.Properties.DataSource = cartype;
            this.lookUpEdit2.Properties.ValueMember = "VehicleTypeId";
            this.lookUpEdit2.Properties.DisplayMember = "VehicleTypeName";

            //驱动性质
            var qudongtype = await DBHelper.Query<DriveTypeInFo>();
            this.lookUpEdit3.Properties.DataSource = qudongtype;
            this.lookUpEdit3.Properties.DisplayMember = "DriveTypeName";
            this.lookUpEdit3.Properties.ValueMember = "DriveTypeId";


            //越野车G类
            var yueyechetype = await DBHelper.Query<OffVehicleClassGInFo>();
            this.lookUpEdit4.Properties.DataSource = yueyechetype;
            this.lookUpEdit4.Properties.DisplayMember = "OffVehicleClassGInFoName";
            this.lookUpEdit4.Properties.ValueMember = "OffVehicleClassGInFoId";

            //燃料类型
            var ranliaoyype = await DBHelper.Query<FuelType>();
            this.lookUpEdit5.Properties.DataSource = ranliaoyype;
            this.lookUpEdit5.Properties.DisplayMember = "FuelTypeName";
            this.lookUpEdit5.Properties.ValueMember = "FuelTypeId";

            //变速器型式
            var biansuqitype = await DBHelper.Query<TransmissionTypeInFo>();
            this.lookUpEdit6.Properties.DataSource = biansuqitype;
            this.lookUpEdit6.Properties.DisplayMember = "TransmissionTypeName";
            this.lookUpEdit6.Properties.ValueMember = "TransmissionTypeId";

            //变速器档位数
            var biansuqidengwuetype = await DBHelper.Query<TransmissionGearsNumberInFo>();
            this.lookUpEdit7.Properties.DataSource = biansuqidengwuetype;
            this.lookUpEdit7.Properties.DisplayMember = "TransmissionGearsNumberName";
            this.lookUpEdit7.Properties.ValueMember = "TransmissionGearsNumberId";
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void simpleButton1_Click(object sender, EventArgs e)
        {
            // 增加1，只影响最低位
            count++;

            int n= 0;;
            try
            {
                #region 一表
                /// <summary>
                /// 是否进口汽车
                /// </summary>
                var IsImportedCarss = this.lookUpEdit1.EditValue.ToString();

                /// <summary>
                /// 汽车生产企业
                /// </summary>
                var AutomobileManufacturerss = textEdit1.Text.ToString();

                /// <summary>
                /// 进口汽车总经销商
                /// </summary>
                var Dealers = textEdit2.Text.ToString();

                /// <summary>
                /// 检测机构名称
                /// </summary>
                var DetectionNames = textEdit3.Text.ToString();

                /// <summary>
                /// 报告编号
                /// </summary>
                var ReportNos = textEdit4.Text.ToString();

                /// <summary>
                /// 备案号
                /// </summary>
                var ICPRecordNumbers = textEdit5.Text.ToString();

                /// <summary>
                /// 车辆型号
                /// </summary>
                var VehicleModels = textEdit6.Text.ToString();

                /// <summary>
                /// 车辆种类
                /// </summary>
                var VersionIds = this.lookUpEdit2.EditValue.ToString();

                /// <summary>
                /// 驱动型式
                /// </summary>
                var DrivingTypes = this.lookUpEdit3.EditValue.ToString();
                /// <summary>
                /// 整车整备质量
                /// </summary>
                var Qualitys = textEdit7.Text.ToString();
                /// <summary>
                /// 最高车速
                /// </summary>
                var TopSpeeds = textEdit8.Text.ToString();

                /// <summary>
                /// 轮胎规格
                /// </summary>
                var TyrSpecifications = textEdit9.Text.ToString();

                /// <summary>
                /// 轴距
                /// </summary>
                var Wheelbases = textEdit10.Text.ToString();

                /// <summary>
                /// 车辆制造日期/进口日期
                /// </summary>
                var DateOfImports = dateTimeOffsetEdit1.EditValue.ToString();

                /// <summary>
                /// 通用名称
                /// </summary>
                var GenericNames = textEdit11.Text.ToString();

                /// <summary>
                ///越野车种G类
                /// </summary>
                var TypeIdNames = this.lookUpEdit4.EditValue.ToString();

                /// <summary>
                ///座位排数
                /// </summary>
                var NumberOfSeatss = textEdit12.Text.ToString();

                /// <summary>
                ///最大设计总质量
                /// </summary>
                var MaximumTotalDesignMasss = textEdit13.Text.ToString();

                /// <summary>
                ///额定载客
                /// </summary>
                var RatedCapacitys = textEdit14.Text.ToString();

                /// <summary>
                ///轮距(前/后)
                /// </summary>
                var Treads = textEdit15.Text.ToString();

                /// <summary>
                ///燃料类型
                /// </summary>
                var FuelTypeIds = this.lookUpEdit5.EditValue.ToString();

                /// <summary>
                /// 反馈码
                /// </summary>
                var FeedbackCodes = $"YH{count.ToString("0000000000000000")}";

                /// <summary>
                /// 状态码
                /// </summary>
                var Statuss = 1;

                ///日期
                var DateInfos = DateTime.Now.ToString();


                var vehicinfor = new BasicInformation { IsImportedCars = Convert.ToInt32(IsImportedCarss), AutomobileManufacturers = AutomobileManufacturerss, Dealer = Dealers, DetectionName = DetectionNames, ReportNo = ReportNos, ICPRecordNumber = ICPRecordNumbers, VehicleModel = VehicleModels, VersionId = Convert.ToInt32(VersionIds), DrivingType = Convert.ToInt32(DrivingTypes), Quality = Qualitys, TopSpeed = TopSpeeds, TyrSpecification = TyrSpecifications, Wheelbase = Wheelbases, DateOfImport = Convert.ToDateTime(DateOfImports), GenericName = GenericNames, TypeIdName = Convert.ToInt32(TypeIdNames), NumberOfSeats = NumberOfSeatss, MaximumTotalDesignMass = MaximumTotalDesignMasss, RatedCapacity = RatedCapacitys, Tread = Treads, FuelTypeId = Convert.ToInt32(FuelTypeIds), FeedbackCode = FeedbackCodes, Status = Statuss };
                DBHelper.Db.Ado.BeginTran();
                #endregion
                n = await DBHelper.Insert(vehicinfor);

                Statuss = 3;
                n = await DBHelper.Update(vehicinfor);

                await DBHelper.Db.Ado.CommitTranAsync();
                //sqltran.Commit();//执行完要提交
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);//抛出的异常消息
                                            //sqltran.Rollback();//如果某条sql语句执行不成功，回滚
                await DBHelper.Db.Ado.RollbackTranAsync();
            }



           

            // 格式化为16位的字符串，并返回

           


             

            if (n > 0)
            {
                MessageBox.Show("保存成功");
            }

        }

        /// <summary>
        /// 油耗类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void lookUpEdit5_EditValueChanged(object sender, EventArgs e)
        {
            if (lookUpEdit5.EditValue != null)
            {
                int valuesinfo = Convert.ToInt32(lookUpEdit5.EditValue);

                switch (valuesinfo)
                {
                    case 1:
                        var onrlistTask = DBHelper.QueryC<FuelParameters>(x => x.FuelTypeId == 1);
                        var onrlist = await onrlistTask;
                        foreach (var item in onrlist)
                        {
                            lookUpEdit6.EditValue = item.TransmissionTypeId;
                            lookUpEdit7.EditValue = item.BanSuQiDang;

                            textEdit16.Text = item.EngineModel;
                            textEdit17.Text = item.FuelParametersName;
                            textEdit18.Text = item.NumberOfCylinders;
                            textEdit19.Text = item.FuelConsumptionInSuburbanConditions;
                            textEdit20.Text = item.CombinedOperatingConditionC02Emissions;
                            textEdit21.Text = item.RatedPower;
                            textEdit22.Text = item.NetPower;
                            textEdit23.Text = item.AutomotiveEnergyTechnology;
                            textEdit24.Text = item.iNfoOther;
                            textEdit25.Text = item.FuelConsumptionInUrbanConditions;
                            textEdit26.Text = item.FuelConsumptionUnderComprehensiveOperatingConditions;
                        }
                        break;
                    case 2:
                        var onrlistTask2 = DBHelper.QueryC<FuelParameters>(x => x.FuelTypeId == 2);
                        var onrlisttwo = await onrlistTask2;
                        foreach (var item in onrlisttwo)
                        {
                            lookUpEdit6.EditValue = item.TransmissionTypeId;
                            lookUpEdit7.EditValue = item.BanSuQiDang;

                            textEdit16.Text = item.EngineModel;
                            textEdit17.Text = item.FuelParametersName;
                            textEdit18.Text = item.NumberOfCylinders;
                            textEdit19.Text = item.FuelConsumptionInSuburbanConditions;
                            textEdit20.Text = item.CombinedOperatingConditionC02Emissions;
                            textEdit21.Text = item.RatedPower;
                            textEdit22.Text = item.NetPower;
                            textEdit23.Text = item.AutomotiveEnergyTechnology;
                            textEdit24.Text = item.iNfoOther;
                            textEdit25.Text = item.FuelConsumptionInUrbanConditions;
                            textEdit26.Text = item.FuelConsumptionUnderComprehensiveOperatingConditions;
                        }
                        break;
                    case 3:
                        var onrlistTask3 = DBHelper.QueryC<FuelParameters>(x => x.FuelTypeId == 3);
                        var onrlisthree = await onrlistTask3;
                        foreach (var item in onrlisthree)
                        {
                            lookUpEdit6.EditValue = item.TransmissionTypeId;
                            lookUpEdit7.EditValue = item.BanSuQiDang;

                            textEdit16.Text = item.EngineModel;
                            textEdit17.Text = item.FuelParametersName;
                            textEdit18.Text = item.NumberOfCylinders;
                            textEdit19.Text = item.FuelConsumptionInSuburbanConditions;
                            textEdit20.Text = item.CombinedOperatingConditionC02Emissions;
                            textEdit21.Text = item.RatedPower;
                            textEdit22.Text = item.NetPower;
                            textEdit23.Text = item.AutomotiveEnergyTechnology;
                            textEdit24.Text = item.iNfoOther;
                            textEdit25.Text = item.FuelConsumptionInUrbanConditions;
                            textEdit26.Text = item.FuelConsumptionUnderComprehensiveOperatingConditions;
                        }
                        break;
                }
            }

        }

        /// <summary>
        /// 保存并上报
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void simpleButton2_Click(object sender, EventArgs e)
        {
          
            // 增加1，只影响最低位
            counts++;


            #region 一表
            /// <summary>
            /// 是否进口汽车
            /// </summary>
            var IsImportedCarss = this.lookUpEdit1.EditValue.ToString();

            /// <summary>
            /// 汽车生产企业
            /// </summary>
            var AutomobileManufacturerss = textEdit1.Text.ToString();

            /// <summary>
            /// 进口汽车总经销商
            /// </summary>
            var Dealers = textEdit2.Text.ToString();

            /// <summary>
            /// 检测机构名称
            /// </summary>
            var DetectionNames = textEdit3.Text.ToString();

            /// <summary>
            /// 报告编号
            /// </summary>
            var ReportNos = textEdit4.Text.ToString();

            /// <summary>
            /// 备案号
            /// </summary>
            var ICPRecordNumbers = textEdit5.Text.ToString();

            /// <summary>
            /// 车辆型号
            /// </summary>
            var VehicleModels = textEdit6.Text.ToString();

            /// <summary>
            /// 车辆种类
            /// </summary>
            var VersionIds = this.lookUpEdit2.EditValue.ToString();

            /// <summary>
            /// 驱动型式
            /// </summary>
            var DrivingTypes = this.lookUpEdit3.EditValue.ToString();
            /// <summary>
            /// 整车整备质量
            /// </summary>
            var Qualitys = textEdit7.Text.ToString();
            /// <summary>
            /// 最高车速
            /// </summary>
            var TopSpeeds = textEdit8.Text.ToString();

            /// <summary>
            /// 轮胎规格
            /// </summary>
            var TyrSpecifications = textEdit9.Text.ToString();

            /// <summary>
            /// 轴距
            /// </summary>
            var Wheelbases = textEdit10.Text.ToString();

            /// <summary>
            /// 车辆制造日期/进口日期
            /// </summary>
            var DateOfImports = dateTimeOffsetEdit1.EditValue.ToString();

            /// <summary>
            /// 通用名称
            /// </summary>
            var GenericNames = textEdit11.Text.ToString();

            /// <summary>
            ///越野车种G类
            /// </summary>
            var TypeIdNames = this.lookUpEdit4.EditValue.ToString();

            /// <summary>
            ///座位排数
            /// </summary>
            var NumberOfSeatss = textEdit12.Text.ToString();

            /// <summary>
            ///最大设计总质量
            /// </summary>
            var MaximumTotalDesignMasss = textEdit13.Text.ToString();

            /// <summary>
            ///额定载客
            /// </summary>
            var RatedCapacitys = textEdit14.Text.ToString();

            /// <summary>
            ///轮距(前/后)
            /// </summary>
            var Treads = textEdit15.Text.ToString();

            /// <summary>
            ///燃料类型
            /// </summary>
            var FuelTypeIds = this.lookUpEdit5.EditValue.ToString();

            /// <summary>
            /// 反馈码
            /// </summary>
            var FeedbackCodes = $"YH{counts.ToString("0000000000000000")}";

            /// <summary>
            /// 状态码
            /// </summary>
            var Statuss = 2;

            ///日期
            var DateInfoss = DateTime.Now.ToString();

            Fuels fuelParameters = new(Convert.ToInt32(IsImportedCarss), AutomobileManufacturerss, Dealers, DetectionNames, ReportNos, ICPRecordNumbers, VehicleModels, Convert.ToInt32(VersionIds), Convert.ToInt32(DrivingTypes), Qualitys, TopSpeeds, TyrSpecifications, Wheelbases, Convert.ToDateTime(DateOfImports), GenericNames, Convert.ToInt32(TypeIdNames), NumberOfSeatss, MaximumTotalDesignMasss, RatedCapacitys, Treads, Convert.ToInt32(FuelTypeIds), FeedbackCodes, Statuss, Convert.ToDateTime(DateInfoss));


            #endregion
            var result = await HttpClientHelper.RequestUrl<Fuels, ApiResult<string>>("https://localhost:44341/", "api/app/fuel-synchronization/basic-inform", MethodType.Post, fuelParameters);

            if (result.Code == 200)
            {
                FeedbackCode feedbackCode = new FeedbackCode(ICPRecordNumbers, FeedbackCodes);
                feedbackCode.Show();
            }

            //Program.UnWatchFiles(); // 停止监视文件变化
        }

        public record Fuels(int isImportedCars, string automobileManufacturers, string dealer, string detectionName, string reportNo, string icpRecordNumber, string vehicleModel, int versionId, int drivingType, string quality, string topSpeed, string tyrSpecification, string wheelbase, DateTime dateOfImport, string genericName, int typeIdName, string numberOfSeats, string maximumTotalDesignMass, string ratedCapacity, string tread, int fuelTypeId, string feedbackCode, int status, DateTime DateInfos);


      
    }



}
